﻿using System;
using System.Collections.Generic;
using System.Linq;
using StockCrawlerLib.DAL;
using StockCrawlerLib;
using CrawlerCore;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System.Globalization;

namespace CrawlerController
{
    public class BaseCrawler
    {
        protected HistoryRepository _historyRepository = new HistoryRepository();
        protected StockRepository _stockRepository = new StockRepository();

        /// <summary>
        /// Kiểm tra history đã tồn tại chưa? 
        /// </summary>
        /// <param name="id_stock"></param>
        /// <param name="ngay_giao_dich"></param>
        /// <returns></returns>
        public bool InitHistory(string currCode, int id_stockexchange, DateTime ngay_giao_dich, out history objHistory)
        {
            bool isExist = false;
            objHistory = new history();
            stock objStock = new stock();
            if (_stockRepository.IsExists(currCode))
                objStock = _stockRepository.Select(currCode);
            else
            {
                objStock.code = currCode;
                objStock.id_stockexchange = id_stockexchange;
                objStock.status = true;
                objStock.isRemark = false;
                _stockRepository.Insert(objStock);
            }

            //Kiểm tra history này đã có trong CSDL chưa
            if (_historyRepository.IsExist(objStock.id_stock, ngay_giao_dich))
            {
                var query = _historyRepository.SelectRecord(objStock.id_stock, ngay_giao_dich);
                foreach (history obj in query)
                    objHistory = obj;
                isExist = true;
            }
            else
            {
                objHistory.id_stock = objStock.id_stock;
                objHistory.ngay_giao_dich = ngay_giao_dich;
            }
            return isExist;
        }

        public bool InitHistory(string currCode, DateTime ngay_giao_dich, out history objHistory)
        {
            bool isExist = false;
            objHistory = new history();
            stock objStock = new stock();
            objStock = _stockRepository.Select(currCode);
            
            //Kiểm tra history này đã có trong CSDL chưa
            if (_historyRepository.IsExist(objStock.id_stock, ngay_giao_dich))
            {
                var query = _historyRepository.SelectRecord(objStock.id_stock, ngay_giao_dich);
                foreach (history obj in query)
                    objHistory = obj;
                isExist = true;
            }
            else
            {
                objHistory.id_stock = objStock.id_stock;
                objHistory.ngay_giao_dich = ngay_giao_dich;
            }
            return isExist;
        }

        /// <summary>
        /// Tổng hợp lịch sử giao dịch
        /// </summary>
        /// <param name="currCode"></param>
        public virtual void ImportPriceHisotry(string currCode, DateTime dtStart)
        {
           
        }

        /// <summary>
        /// Tổng hợp lịch sử khớp lệnh theo link 
        /// </summary>
        /// <param name="currCode"></param>
        public virtual void ImportOrderHisotry(string currCode, DateTime dtStart)
        {
            
        }

        /// <summary>
        /// Giao dịch nước ngoài
        /// </summary>
        /// <param name="currCode"></param>
        /// <param name="dtStart"></param>
        public virtual void ImportForeignHistory(DateTime dtStart)
        {

        }

        /// <summary>
        /// Giao dịch thỏa thuận nước ngoài
        /// </summary>
        /// <param name="currCode"></param>
        /// <param name="dtStart"></param>
        public virtual void ImportForeign2History(DateTime dtStart)
        {

        }

        public virtual string GetCurrentInfo(string currCode)
        {
            return "";
        }

    }
}

